var Finace;
if (!Finace) {
    Finace = {}
}


var typeFormatter = function (value, row, index) {
    if (value == "全部") {
        return '全部';
    } else {
        //第二位是什么
        var secondNum = value.substring(1, 2);
        if (secondNum > 0 && secondNum < 9) {
            return "别名";
        } else {
            return "标签";
        }
    }
}

//添加按钮动作
bindClick("#addPushMsg", function () {

    $("#addPushmsg-div input:radio[value=2]").prop("checked", true);
    $("#div-alias").hide();
    $("#div-tags").hide();
    $("#div-alias textarea").val('');
    var content = UE.getEditor('editor').setContent("");
    $("#div-tags input[type=checkbox]").prop("checked", false);
    $("#pushmsgform input[name=title]").val("");
    $("#pushmsgform input[name=remark]").val("");

    showAddPushAdv();

});

//显示右侧的添加页面
var showAddPushAdv = function () {
    showModel("addPushmsg-div", function () {
        // $("#addImage1").attr("img-path", "");
        // $("#addImage1").attr("img-url", "");
        // $("#addImage1").removeAttribute("src");
        // $("#addImage1").attr("src", $.AbsUrlParse() + "/static/images/commons/img_add.png");
        $("#tags1").val('用户端');
        $("#tags2").val('商家端');
        $("#tags3").val('配送端');
    });
}

$(document).on("change", "input[name=pushType]", function () {
    var val = $(this).val();

    var tag = $("#div-tags");
    var alia = $("#div-alias");

    if (val == 0) {
        //别名
        tag.hide();
        alia.show();
    } else if (val == 1) {
        //标签
        alia.hide();
        tag.show();
    } else {
        //全部
        alia.hide();
        tag.hide();
    }
    return false;
});

//保存右侧页面数据
bindClick("button[data-event=submit-savepushmsg]", function (tag) {
    var content = UE.getEditor('editor').getContent();
    var title = $("#pushmsgform input[name=title]").val();
    var remark = $("#pushmsgform input[name=remark]").val();


    // if (title == "" || title.trim() == "") {
    //     hint("标题没有填写");
    //     return false;
    // }
    // if (remark == "" || remark.trim() == "") {
    //     hint("摘要没有填写");
    //     return false;
    // }
    // if (content == "" || content.trim() == "") {
    //     hint("发布信息没有填写");
    //     return false;
    // }


    $("#content").text(content);
    var path = $("#addImage1").attr("img-path");
    $("input[name=picture]").val(path);

    var form = $("#pushmsgform");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();


    var tags = "";
    $("#div-tags input[type=checkbox]:checked").each(function () {
        tags += $(this).val() + ",";
    });
    if (tags != "") {
        tags = tags.substring(0, tags.length - 1);
    }


    comment.post($().AbsUrlParse("/web/push/add?tags=" + tags + ""), params, null, function (res) {
        if (res.res.code == 40000) {
            form[0].reset();
            //关闭model方法
            closeModel("addPushmsg-div");
            var table = $("#table");
            table.bootstrapTable("refresh");


        }
    });
});

//操作格式函数
operateFormatter = function (value, row, index) {
    return ['<a class="find" href="javascript:void(0)" title="查看">查看</a>&nbsp;&nbsp;',
        '<a class="remove" href="javascript:void(0)" style="color: red">删除</a>&nbsp;&nbsp;'].join('');
}

//操作按鈕动作
window.operateEvents = {
    'click .find': function (e, value, row, index) {
        showModel("findpushmsg-div", function () {
            $("#title").val(row.title);
            $("#remark").val(row.remark);
            if (row.picture != '' || row.pictrue != null) {
                $("#picture").attr("src", File_Download_Url + "/" + row.picture);
            }
            $("#content1").html(row.content);
        });
    },
    'click .remove': function (e, value, row, index) {
        comment.showOperationTips("确定删除本条记录", function () {
            comment.post($().AbsUrlParse("/web/push/delete"), {id: row.id}, null, function (data) {
                if (data.res.code = 40000) {
                    var table = $("#table");
                    table.bootstrapTable("refresh");
                }
            });
        });
    }
};

//推送查询

bindClick("#tuisongSear", function () {
    var tuisongtitle = $("input[name=tuisongtitle]").val();
    var tuisongren = $("select[name=tuisongren]").val();

    var tuisongtime = $("input[name=tuisongtime]").val();

    var xjstart = "";
    var xjend = "";

    if (tuisongtime != "") {
        xjstart = tuisongtime.split(" - ")[0];
        xjend = tuisongtime.split(" - ")[1];
    }

    var url = $().AbsUrlParse("/web/push/pushQuery?tuisongtitle=" + tuisongtitle + "&tuisongren=" + tuisongren + "&xjstart=" + xjstart + "&xjend=" + xjend );
    $("#table").bootstrapTable("refresh", {url: url});
})

//初始化页面的时候运行该函数
$(function () {
    initDateRange();
});

function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}
